import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../components/Member.vue'
import Login from '../views/Login.vue'
import bookorder from '../components/bookorder.vue'
import inventory from '../components/inventory.vue'
import bookrecord from '../components/bookrecord.vue'
import Permissions from '../components/Permissions.vue'
import store from '@/store'


Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    meta: { requiresAuth: true }, 
  },
  {
    path: '/bookorder',
    name: 'bookorder',
    component: bookorder,
    meta: { requiresAuth: true }, 
  },
  {
    path: '/',
    name: 'Login',
    component: Login,
  },
  {
    path: '/inventory',
    name: 'inventory',
    component: inventory,
    meta: { requiresAuth: true },
  },
  {
    path: '/bookrecord',
    name: 'bookrecord',
    component: bookrecord,
    meta: { requiresAuth: true }, 
  },
  {
    path: '/Permissions',
    name: 'Permissions',
    component: Permissions,
    meta: { requiresAuth: true },
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((home, Login, next) => {
  if (home.matched.some(record => record.meta.requiresAuth)) {
    // 检查Vuex状态
    if (store.state.key) {
      next();  // 允许访问
    } else {
      next('/Login');  // 重定向到登录页
    }
  } else {
    next();  // 确保正常进行其它路由
  }
});
router.beforeEach((inventory, Login, next) => {
  if (inventory.matched.some(record => record.meta.requiresAuth)) {
    // 检查Vuex状态
    if (store.state.key) {
      next();  // 允许访问
    } else {
      next('/Login');  // 重定向到登录页
    }
  } else {
    next();  // 确保正常进行其它路由
  }
});
router.beforeEach((bookrecord, Login, next) => {
  if (bookrecord.matched.some(record => record.meta.requiresAuth)) {
    // 检查Vuex状态
    if (store.state.key) {
      next();  // 允许访问
    } else {
      next('/Login');  // 重定向到登录页
    }
  } else {
    next();  // 确保正常进行其它路由
  }
});
router.beforeEach((Permissions, Login, next) => {
  if (Permissions.matched.some(record => record.meta.requiresAuth)) {
    // 检查Vuex状态
    if (store.state.key) {
      next();  // 允许访问
    } else {
      next('/Login');  // 重定向到登录页
    }
  } else {
    next();  // 确保正常进行其它路由
  }
});
export default router
